<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>视频会议应用</title>
    
    <!-- 引入mediasoupClient -->
    <script type="module">
        import * as mediasoupClient from 'mediasoup-client';
        console.log('mediasoupClient loaded:', mediasoupClient);
        window.mediasoupClient = mediasoupClient;
    </script>
    
    <!-- 引入Font Awesome图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- 引入Bootstrap -->
    <link href="modules/bootstrap.min.css" rel="stylesheet" />
    
    <!-- 自定义样式 -->
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --accent-color: #4895ef;
            --light-color: #f8f9fa;
            --dark-color: #212529;
            --success-color: #4cc9f0;
            --danger-color: #f72585;
            --border-radius: 8px;
            --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }
        
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: var(--dark-color);
            line-height: 1.6;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 15px;
        }
        
        .card {
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            padding: 25px;
            margin-bottom: 25px;
            transition: var(--transition);
        }
        
        .card:hover {
            box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            color: var(--primary-color);
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #e9ecef;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .input-group {
            display: flex;
            flex-direction: column;
            margin-bottom: 15px;
        }
        
        .input-group label {
            font-weight: 600;
            margin-bottom: 8px;
            color: var(--dark-color);
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .input-field {
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: var(--border-radius);
            font-size: 16px;
            transition: var(--transition);
        }
        
        .input-field:focus {
            border-color: var(--accent-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(72, 149, 239, 0.2);
        }
        
        .btn {
            padding: 12px 20px;
            border: none;
            border-radius: var(--border-radius);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }
        
        .btn-primary:hover {
            background-color: var(--secondary-color);
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-2px);
        }
        
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background-color: #218838;
            transform: translateY(-2px);
        }
        
        .btn-danger {
            background-color: var(--danger-color);
            color: white;
        }
        
        .btn-danger:hover {
            background-color: #e11571;
            transform: translateY(-2px);
        }
        
        .btn-info {
            background-color: var(--accent-color);
            color: white;
        }
        
        .btn-info:hover {
            background-color: #3a86e4;
            transform: translateY(-2px);
        }
        
        .btn-group {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 15px;
        }
        
        .hidden {
            display: none;
        }
        
        .media-controls {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 20px;
        }
        
        .devices-selector {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin-top: 20px;
        }
        
        .device-select {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .device-select label {
            font-weight: 600;
            min-width: 80px;
        }
        
        .form-select {
            padding: 10px 15px;
            border: 2px solid #e9ecef;
            border-radius: var(--border-radius);
            background-color: white;
            flex-grow: 1;
        }
        
        .media-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .media-box {
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            overflow: hidden;
            transition: var(--transition);
        }
        
        .media-box:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
        }
        
        .media-header {
            background: var(--primary-color);
            color: white;
            padding: 10px 15px;
            font-weight: 600;
        }
        
        .media-content {
            padding: 15px;
            min-height: 200px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f8f9fa;
        }
/* 
        .audio-content {
            padding: 15px;
            min-height: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f8f9fa;
        } */

        .voice-activity-indicator {
            display: flex;
            align-items: center;
            margin-right: 8px;
        }
        .voice-bar {
            width: 3px;
            height: 12px;
            background: #4CAF50;
            margin: 0 1.5px;
            border-radius: 1px;
            animation: voice-pulse 1.2s infinite ease-in-out;
        }
        .voice-bar:nth-child(1) { animation-delay: 0.1s; }
        .voice-bar:nth-child(2) { animation-delay: 0.2s; }
        .voice-bar:nth-child(3) { animation-delay: 0.3s; }
        .voice-bar:nth-child(4) { animation-delay: 0.4s; }
        .voice-bar:nth-child(5) { animation-delay: 0.5s; }
        @keyframes voice-pulse {
            0%, 100% { transform: scaleY(1); }
            50% { transform: scaleY(2.5); }
        }
        
        .login-form {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: flex-end;
        }
        
        @media (max-width: 768px) {
            .login-form {
                flex-direction: column;
                align-items: stretch;
            }
            
            .media-controls {
                grid-template-columns: 1fr;
            }
            
            .media-container {
                grid-template-columns: 1fr;
            }
        }
        
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
        }
        
        .status-online {
            background-color: #28a745;
        }
        
        .status-offline {
            background-color: #dc3545;
        }

        /* 视频流容器 - 响应式网格布局 */
        .video-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 16px;
            padding: 16px;
            width: 100%;
            max-width: 100%;
            box-sizing: border-box;
        }

        /* 单个视频项 */
        .video-item {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        .member-item {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        .member-header {
            padding: 12px 16px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
            font-weight: 500;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .video-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
        }

        /* 媒体头部 */
        .media-header {
            padding: 12px 16px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
            font-weight: 500;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        /* 状态指示器 */
        .status-indicator {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #6c757d;
        }

        .status-online {
            background: #28a745;
            box-shadow: 0 0 0 2px rgba(40, 167, 69, 0.2);
        }

        /* 媒体内容区域 */
        .media-content {
            position: relative;
            width: 100%;
            aspect-ratio: 16 / 9; /* 保持16:9比例 */
        }

        /* 视频包装器 */
        .video-wrapper {
            background: #e9ecef;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
            position: relative;
        }

        /* 视频元素 */
        .vid {
            width: 100%;
            height: 100%;
            object-fit: cover; /* 或者使用 contain 根据需求 */
            background: #000;
        }

        /* 加载状态 */
        .video-wrapper::before {
            content: '视频加载中...';
            position: absolute;
            z-index: 1;
            font-size: 14px;
            display: none;
        }

        .vid:not([src]) ~ .video-wrapper::before {
            display: block;
        }

        /* 响应式设计 - 桌面端 */
        @media (min-width: 1024px) {
            .video-container {
                grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
                gap: 20px;
            }
        }

        /* 平板端 */
        @media (max-width: 1023px) and (min-width: 768px) {
            .video-container {
                grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
                gap: 16px;
            }
        }

        /* 移动端 - 独占一行 */
        @media (max-width: 767px) {
            .video-container {
                grid-template-columns: 1fr;
                gap: 12px;
                padding: 12px;
            }
            
            .media-header {
                padding: 10px 12px;
                font-size: 13px;
            }
            
            .video-item {
                border-radius: 6px;
            }
        }

        /* 超大屏幕优化 */
        @media (min-width: 1440px) {
            .video-container {
                grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
                max-width: 1400px;
                margin: 0 auto;
            }
        }

        /* 确保视频容器有最小高度 */
        .video-container:empty::before {
            content: '暂无视频流';
            display: flex;
            align-items: center;
            justify-content: center;
            height: 200px;
            color: #6c757d;
            font-size: 16px;
            grid-column: 1 / -1;
        }
    </style>
    
    <script src="modules/socket.io.min.js"></script>
    <script src="RoomClient.js"></script>
    <script src="modules/bootstrap.bundle.min.js"></script>
</head>
<body>
    <div class="container">
        <div id="login" class="card">
            <h2 class="section-title">
                <i class="fas fa-video"></i> 加入会议
            </h2>
            <div class="login-form">
                <div class="input-group">
                    <label for="roomidInput">
                        <i class="fas fa-server"></i> 房间号
                    </label>
                    <input id="roomidInput" class="input-field" value="123" type="text" placeholder="输入房间号" />
                </div>
                <div class="input-group">
                    <label for="nameInput">
                        <i class="fas fa-user"></i> 用户名
                    </label>
                    <input id="nameInput" class="input-field" value="user" type="text" placeholder="输入用户名" />
                </div>
                <button id="joinButton" class="btn btn-primary" onclick="joinRoom(nameInput.value, roomidInput.value)">
                    <i class="fas fa-sign-in-alt"></i> 加入会议
                </button>
            </div>
        </div>
    </div>

    <div class="container">
        <div id="control" class="hidden card">
            <h2 class="section-title">
                <i class="fas fa-cogs"></i> 会议控制
            </h2>
            <div class="btn-group">
                <button id="exitButton" class="btn btn-danger" onclick="rc.exit()">
                    <i class="fas fa-arrow-left"></i> 退出会议
                </button>
                <button id="copyButton" class="btn btn-secondary" onclick="rc.copyURL()">
                    <i class="far fa-copy"></i> 复制链接
                </button>
                <button id="devicesButton" class="btn btn-info" onclick="rc.showDevices()">
                    <i class="fas fa-cogs"></i> 设备设置
                </button>
            </div>
            
            <div class="media-controls">
                <button id="startAudioButton" class="btn btn-success" onclick="rc.produce(RoomClient.mediaType.audio, audioSelect.value)">
                    <i class="fas fa-volume-up"></i> 开启音频
                </button>
                <button id="stopAudioButton" class="btn btn-danger" onclick="rc.closeProducer(RoomClient.mediaType.audio)">
                    <i class="fas fa-volume-mute"></i> 关闭音频
                </button>
                <button id="startVideoButton" class="btn btn-success" onclick="rc.produce(RoomClient.mediaType.video, videoSelect.value)">
                    <i class="fas fa-camera"></i> 开启视频
                </button>
                <button id="stopVideoButton" class="btn btn-danger" onclick="rc.closeProducer(RoomClient.mediaType.video)">
                    <i class="fas fa-camera-slash"></i> 关闭视频
                </button>
                <button id="startScreenButton" class="btn btn-success" onclick="rc.produce(RoomClient.mediaType.screen)">
                    <i class="fas fa-desktop"></i> 共享屏幕
                </button>
                <button id="stopScreenButton" class="btn btn-danger" onclick="rc.closeProducer(RoomClient.mediaType.screen)">
                    <i class="fas fa-stop"></i> 停止共享
                </button>
            </div>
            
            <div id="devicesList" class="hidden devices-selector">
                <h3 class="section-title">
                    <i class="fas fa-sliders-h"></i> 设备选择
                </h3>
                <div class="device-select">
                    <label for="audioSelect">
                        <i class="fas fa-microphone"></i> 音频设备:
                    </label>
                    <select id="audioSelect" class="form-select"></select>
                </div>
                <div class="device-select">
                    <label for="videoSelect">
                        <i class="fas fa-video"></i> 视频设备:
                    </label>
                    <select id="videoSelect" class="form-select"></select>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        <div id="videoMedia" class="hidden">
            <div class="card">
                <h2 class="section-title">
                    <i class="fas fa-user-circle"></i> 我的设备
                </h2>
                <div id="localMedia" class="media-container">
                    <!-- 本地视频和屏幕共享将在这里显示 -->
                </div>
            </div>
            
            <div class="card">
                <h2 class="section-title">
                    <i class="fas fa-users"></i> 正在共享
                </h2>
                <div id="remoteVideos" class="media-container">
                    <!-- 远程视频将在这里显示 -->
                </div>
                <div id="remoteAudios"></div>
            </div>

            <div class="card">
                <h2 class="section-title">
                    <i class="fas fa-users"></i> 全部成员
                </h2>
                <div id="allMembers"></div>
            </div>
        </div>
    </div>
    <script src="config.js"></script>
    <script src="index.js"></script>
</body>
</html>